package com.ph.demo.JDKcurrent;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by peng on 2019/4/10.
 */
public class ReenterlockDemo extends Thread{

    public static final ReentrantLock lock = new ReentrantLock();

    public static  int i = 0;

    @Override
    public void run() {
        for (int j = 0; j <1000000 ; j++) {
            lock.lock();
            //lock.lock();

            try {
                i++;
            } finally {
                lock.unlock();  //一定要释放锁
                //lock.unlock();
            }
        }


    }

    public static void main(String[] args) throws Exception{
        Thread[] ts = new Thread[10];

        for (int j = 0; j <10 ; j++) {
            ts[j] = new ReenterlockDemo();
            ts[j].start();
        }
        for (int j = 0; j <10 ; j++) {
            ts[j].join();
        }
        System.out.println(i);
    }


}
